package com.sduept.nwld.dataview.controller.core;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.ledger.excetpion.ManagementUnitException;
import com.sduept.cim.ledger.model.ManagementUnitDict;
import com.sduept.cim.ledger.services.ManagementUnitService;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import com.sduept.utils.PingYinUtil;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.PrimeFaces;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Named
@ViewScoped
public class DesorgDepartSelectorController extends AbstractController {
	//TODO: 该类 更换了ManagementUnitService 实现，需要优化，暂时把错误去掉
	/**
	 * 
	 */
	private static final long serialVersionUID = 5952630497988638537L;
	
	@Autowired
	private ManagementUnitService unitService;
	
	private TreeNode desorgTree; // 调度单位树
	private TreeNode selectedNode;

	/**
	 * 所有的调度单位，其中pid为null的为父节点
	 */
	private List<ManagementUnitDict> allDispatchUnits;
	
	private String dept; 
	
	@PostConstruct
	public void init() {
		try {
			allDispatchUnits = unitService.getAllDispatchingManagements();
			genDesorgTree();
		} catch (ManagementUnitException e) {
			e.printStackTrace();
			addErrorMessage("获取所有的调管单位失败");
		}
	}
	/**
	 * 构造调度单位树
	 */
	public void genDesorgTree() {
		desorgTree = new LibTreeNode("Root", null);
		try {
			String rootName = unitService.getManagementUnitRoot();
			if(StringUtils.isEmpty(rootName)) {
				rootName = "南方电网";
			}
			LibTreeNode rootNode = new LibTreeNode("root",rootName, desorgTree);
			recursionGenDesorgTree(null, rootNode);
			rootNode.setExpanded(true);
		} catch (ManagementUnitException e) {
			e.printStackTrace();
			addErrorMessage("获取部门根节点失败");
		}
	}
	/**
	 * 递归生成树节点
	 * @param pid
	 * @param root
	 */
	private void recursionGenDesorgTree(String pid,TreeNode root) {
		List<ManagementUnitDict> childs = getUnitsByPidFromList(pid);
		for (ManagementUnitDict unit : childs) {
			LibTreeNode childNode = new LibTreeNode("dispatchUnit",unit, root);
			recursionGenDesorgTree(unit.getId(), childNode);
		}
	}
	/**
	 * 根据pid，从allDispatchUnits中获取pid符合条件的记录
	 * @param pid
	 * @return 
	 */
	private List<ManagementUnitDict> getUnitsByPidFromList(String pid) {
		List<ManagementUnitDict> result = new ArrayList<ManagementUnitDict>();
		if(null == pid) {
			for (ManagementUnitDict unit : allDispatchUnits) {
				if(StringUtils.isEmpty(unit.getPid())) {
					result.add(unit);
				}
			}
		} else {
			for (ManagementUnitDict unit : allDispatchUnits) {
				if(pid.equals(unit.getPid())) {
					result.add(unit);
				}
			}
		}
		sort(result);
		return result;
	}
	/**
	 * 将ManagementUnitDict按ordered从高到低排序
	 * @param List<ManagementUnitDict>
	 */
	private void sort(List<ManagementUnitDict> list) {
		Collections.sort(list,new Comparator<ManagementUnitDict> () {
			@Override
			public int compare(ManagementUnitDict o1, ManagementUnitDict o2) {
				return o2.getOrdered()-o1.getOrdered();
			}
		});
	}
	
	/**
	 * 选中树节点
	 * @param event
	 */
	public void onNodeSelect(NodeSelectEvent event) {
		selectedNode = event.getTreeNode();
	}
	
	/**
	 * 展开树节点
	 * @param event
	 */
	public void onNodeExpand(NodeExpandEvent event) {
//		TreeNode selectedNode = event.getTreeNode();
//		List<ManagementUnitDict> childs = null;
//		if(selectedNode.getData() instanceof String) {
//			childs = getUnitsByPidFromList(null);
//		} else if (selectedNode.getData() instanceof ManagementUnitDict) {
//			ManagementUnitDict pUnitDict = (ManagementUnitDict) selectedNode.getData();
//			childs = getUnitsByPidFromList(pUnitDict.getId());
//		}
//		for (ManagementUnitDict unit : childs) {
//			new LibTreeNode("dispatchUnit",unit, selectedNode);
//		}
	}
	
	/**
	 * 调管单位
	 */
	public List<String> completeDispatchUnit(String query) {
		List<String> result = new ArrayList<String>();
		for (ManagementUnitDict unit : allDispatchUnits) {
			String pName = PingYinUtil.getPingYinHeader(unit.getName()); 
			if (pName.contains(query)) {
				result.add(unit.getName());
			}
		}
		return result;
	}
	
	/**
	 * 调度单位树增加快速筛选功能，根据查询的部门重新构造树
	 * @param event
	 */
	public void autoCompleteOrgTree(SelectEvent event) {
		Object item = event.getObject();
		if (null != item) {
			desorgTree = new DefaultTreeNode();
			try {
				ManagementUnitDict unit = unitService.getManagementUnitByName(item.toString());
				new LibTreeNode("dispatchUnit",unit, desorgTree);
			} catch (ManagementUnitException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 清空快速筛选条件时，需要重新构造树
	 */
	public void refreshOrgTree() {
		dept = null;
		selectedNode = null;
		genDesorgTree();
	}
	/**
	 * 选中某个部门后
	 */
	public void onSelectDesorg() {
		if("dispatchUnit".equals(selectedNode.getType())) {
			ManagementUnitDict select = (ManagementUnitDict) selectedNode.getData();
			PrimeFaces.current().dialog().closeDynamic(select);
		} else if("root".equals(selectedNode.getType())) {
			selectedNode = null;
			alertErrorMessage("请选择正确的部门");
		}
	}
	
	public TreeNode getDesorgTree() {
		return desorgTree;
	}

	public void setDesorgTree(TreeNode desorgTree) {
		this.desorgTree = desorgTree;
	}

	public TreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

}
